<?php

/**
 * Libro form base class.
 *
 * @package    form
 * @subpackage libro
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $
 */
class BaseLibroForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'titulo'             => new sfWidgetFormInput(),
      'numero_coleccion'   => new sfWidgetFormInput(),
      'notas'              => new sfWidgetFormTextarea(),
      'coleccion_libro_id' => new sfWidgetFormDoctrineChoice(array('model' => 'ColeccionLibro', 'add_empty' => true)),
      'editor_id'          => new sfWidgetFormDoctrineChoice(array('model' => 'Editor', 'add_empty' => true)),
      'deudor_id'          => new sfWidgetFormDoctrineChoice(array('model' => 'Deudor', 'add_empty' => true)),
      'lugar_id'           => new sfWidgetFormDoctrineChoice(array('model' => 'Lugar', 'add_empty' => true)),
      'escritores_list'    => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Autor')),
      'generos_list'       => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Genero')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorDoctrineChoice(array('model' => 'Libro', 'column' => 'id', 'required' => false)),
      'titulo'             => new sfValidatorString(array('max_length' => 120)),
      'numero_coleccion'   => new sfValidatorInteger(array('required' => false)),
      'notas'              => new sfValidatorString(array('required' => false)),
      'coleccion_libro_id' => new sfValidatorDoctrineChoice(array('model' => 'ColeccionLibro', 'required' => false)),
      'editor_id'          => new sfValidatorDoctrineChoice(array('model' => 'Editor', 'required' => false)),
      'deudor_id'          => new sfValidatorDoctrineChoice(array('model' => 'Deudor', 'required' => false)),
      'lugar_id'           => new sfValidatorDoctrineChoice(array('model' => 'Lugar', 'required' => false)),
      'escritores_list'    => new sfValidatorDoctrineChoiceMany(array('model' => 'Autor', 'required' => false)),
      'generos_list'       => new sfValidatorDoctrineChoiceMany(array('model' => 'Genero', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('libro[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Libro';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['escritores_list']))
    {
      $this->setDefault('escritores_list', $this->object->Escritores->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['generos_list']))
    {
      $this->setDefault('generos_list', $this->object->Generos->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveEscritoresList($con);
    $this->saveGenerosList($con);
  }

  public function saveEscritoresList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['escritores_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Escritores->getPrimaryKeys();
    $values = $this->getValue('escritores_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Escritores', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Escritores', array_values($link));
    }
  }

  public function saveGenerosList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['generos_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Generos->getPrimaryKeys();
    $values = $this->getValue('generos_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Generos', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Generos', array_values($link));
    }
  }

}
